package com.trainfinder.proxy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class SimpleLoggerImpl implements Logger {

	private SimpleLoggerImpl() {
		// just made it private to prove that it is not being used anywhere
	}
	
	
	OutputStream outpuStream;

	int currentID = -1;

	public void log(PortType portType, final int uniqueID, byte[] data,
			int count) {
		try {


			if (currentID != uniqueID) {
				final File file = new File("C:\\temp\\" + portType + uniqueID
						+ ".html");
				if (outpuStream != null) {
					outpuStream.close();
				}

				outpuStream = new FileOutputStream(file);
				if (!filterOut(new String(data))) {
					outpuStream = new FileOutputStream(file);
				} else {
					// filtered out
				}
				currentID = uniqueID;
			}

			if (outpuStream != null) {

				outpuStream.write(data, 0, count);
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		 
		} catch (IOException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
			 
		}finally {
			if (outpuStream != null) {
				try {
					outpuStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		// System.out.println(portType + " " + uniqueID);
	}

	private boolean filterOut(String x) {
		return (x.contains("google.com") || x.contains(".css")
				|| x.contains(".png") || x.contains(".google")
		// || x.contains(".js")
		|| x.contains(".gif"));

	}
}